<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>axios</title>
  <script src="../js/vue.js"></script>
  <script src="../js/vuex.js"></script>
  <script src="../js/axios.js"></script>
  <script src="../js/qs.js"></script>
</head>
<body>
  <div id="app">
    <ul v-for="item in categories" :key="item.id">
      <li>{{item.id}}</li>
      <li>{{item.name}}</li>
      <li>{{item.parentId}}</li>
    </ul>
  </div>
  <script>
    let qs = Qs
    // 解构辅助函数
    let { mapState, mapActions } = Vuex
    // 配置默认baseURL
    axios.defaults.baseURL= 'http://203.195.246.58:5588'
    let store = new Vuex.Store({
      state: {
        categories: []
      },
      mutations: {
        SET_CATEGIRY(state,payload) {
          state.categories = payload
        }
      },
      actions: {
        // 查询所有栏目信息
        async findAllCategories(context) {
          let res = await axios.get('/category/findAll')
          context.commit('SET_CATEGIRY',res.data.data)
        }
      }
    })
    new Vue({
      el: '#app',
      store,
      data:{},
      computed:{
        ...mapState(['categories'])
      },
      methods: {
        ...mapActions(['findAllCategories'])
      },
      created() {
        // 调用请求方法
        this.findAllCategories();
      }
    })
  </script>
</body>
</html>